package com.itheima.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.itheima.reggie.common.R;
import com.itheima.reggie.entity.ShoppingCart;
import com.itheima.reggie.service.ShoppingCartService;
import com.itheima.reggie.utils.BaseContext;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;

/**
 * 购物车
 */
@Slf4j
@RestController
@RequestMapping("/shoppingCart")
public class ShoppingCartController {
    @Autowired
    private ShoppingCartService shoppingCartService;


    /**
     * 查看购物车
     * 根据用户Id查询
     * 返回购物车中的数据
     * @return
     */
    @GetMapping("/list")
    public R<List<ShoppingCart>> list(){
        //根据用户Id查询
        LambdaQueryWrapper<ShoppingCart> Lqw = new LambdaQueryWrapper<>();
        Lqw.eq(ShoppingCart::getUserId, BaseContext.getCurrentId());

        //调用方法查询
        List<ShoppingCart> list = shoppingCartService.list(Lqw);
        //返回购物车中的数据
        return R.success(list);
    }

    /**
     * 添加到购物车（套餐setmealId 或 菜品dishId）
     * 设置用户Id指定当前
     *接受数据 将数据封装到数据库中
     * @return
     */
    @PostMapping("/add")
    public R<ShoppingCart> add(@RequestBody ShoppingCart shoppingCart){
        log.info("添加传回来的Id:{}",shoppingCart);
        //设置用户id，指定当前是哪个用户的购物车数据
        Long currentId = BaseContext.getCurrentId();
        shoppingCart.setUserId(currentId);

        //调用方法比对
        LambdaQueryWrapper<ShoppingCart> lqw = new LambdaQueryWrapper<>();
        lqw.eq(currentId != null, ShoppingCart::getUserId, currentId);

        //判断用户输入的是菜品  判断购物车中是否存在
        lqw.eq(shoppingCart.getDishId() != null, ShoppingCart::getDishId, shoppingCart.getDishId());

        //套餐
        lqw.eq(shoppingCart.getSetmealId() != null, ShoppingCart::getSetmealId, shoppingCart.getSetmealId());

        //查询数据库中是否存在
        ShoppingCart shoppingsetWithDish = shoppingCartService.getOne(lqw);
        if (shoppingsetWithDish != null) {
            //说明已存在  原数量 +1
            shoppingsetWithDish.setNumber(shoppingsetWithDish.getNumber() + 1);
            //调用方法修改
            shoppingCartService.updateById(shoppingsetWithDish);
            return R.success(shoppingsetWithDish);
        }else {
            //如果不存在就设置为1  添加购物车
            shoppingCart.setUserId(currentId);
            shoppingCart.setNumber(1);
            shoppingCart.setCreateTime(LocalDateTime.now());
            shoppingCartService.save(shoppingCart);
            return R.success(shoppingCart);
        }
    }


    /**
     * 清空购物车
     *  根据用户Id直接删除
     *  *
     * @return
     */
    @DeleteMapping("/clean")
    public R<String> clean(){
        //根据Id判断 删除
        LambdaQueryWrapper<ShoppingCart> Lqw = new LambdaQueryWrapper<>();
        Lqw.eq(ShoppingCart::getUserId, BaseContext.getCurrentId());

        //调用方法删除
        shoppingCartService.remove(Lqw);
        return R.success("删除成功");
    }

    /**
     * 删除购物车里的
     * 1.根据传回来的id查找 数据库里的Id
     * 2.获取到数据库对象
     * 3.将对象的number（数量）-1
     * 4.判断 如果number 为0的话就从购物车删除
     * @param shoppingCart
     * @return
     */
    @PostMapping("sub")
    public R<ShoppingCart> sub(@RequestBody ShoppingCart shoppingCart){
        //获取用户
        Long currentId = BaseContext.getCurrentId();

        //1.根据传回来的id查找 数据库里的Id
        LambdaQueryWrapper<ShoppingCart> shopLqw = new LambdaQueryWrapper<>();
        shopLqw.eq(shoppingCart.getSetmealId() != null, ShoppingCart::getSetmealId, shoppingCart.getSetmealId());
        shopLqw.eq(shoppingCart.getDishId() != null, ShoppingCart::getDishId, shoppingCart.getDishId());
        //判断用户
        shopLqw.eq(currentId != null, ShoppingCart::getUserId, currentId);

        //2.获取到数据库对象
        ShoppingCart shop = shoppingCartService.getOne(shopLqw);

        if(shop.getNumber() == 1){
            //4.判断 如果number 为0的话就从购物车删除
            shop.setNumber(0);
            shoppingCartService.removeById(shop);
            return R.success(shop);
    }
        //3.将对象的number（数量）-1
        shop.setNumber(shop.getNumber()-1);

        shoppingCartService.updateById(shop);
        return R.success(shop);
    }
 }   